import Promise from 'bluebird';

import service from '../service/user';
import CONSTANT from '../constant/user';

// 获取用户列表
const getUserList = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getUserList(data).then((res) => {
      commit(CONSTANT.GET_USER_SUCCESS, res.applicationUserResponses);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GET_USER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

// 根据搜索条件获取用户列表
const getUserListSearch = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getUserSearch(data).then((res) => {
      commit(CONSTANT.GET_USER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GET_USER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

// 获取用户详情
const getUserDetail = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getUserDetail(data).then((res) => {
      commit(CONSTANT.GET_USER_DETAIL_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GET_USER_DETAIL_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};
// 添加用户
const addUser = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.addUser(data).then((res) => {
      commit(CONSTANT.ADD_USER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.ADD_USER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

// 编辑用户
const editUser = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.editUser(data).then((res) => {
      commit(CONSTANT.EDIT_USER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.EDIT_USER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

// 重置密码
const resetPassword = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.resetPassword(data).then((res) => {
      commit(CONSTANT.RESET_PASSWORD_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.RESET_PASSWORD_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

// 设置用户信息
const editUserInfo = ({ commit }, data) => {
  commit(CONSTANT.SET_EDIT_INFO, data);
};

// 获取公众号列表 （废弃接口
const getPublicAccounts = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getPublicAccounts(data).then((res) => {
      commit(CONSTANT.GET_PUBLIC_ACCOUNTS_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GET_PUBLIC_ACCOUNTS_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};
// 改变用户状态
const changeStatus = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.changeStatus(data).then((res) => {
      commit(CONSTANT.CHANGE_USER_STATUS_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.CHANGE_USER_STATUS_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

export default {
  getUserList,
  getUserDetail,
  addUser,
  editUser,
  resetPassword,
  editUserInfo,
  getPublicAccounts,
  changeStatus,
  getUserListSearch,
};
